| Conditions | 2 |
| Paths | 2 |
| Total Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // Copyright © 2017 Tangdongxin |
||
| 124 | function draw (str, current, isEmbed = false) { |
||
| 125 | |||
| 126 | const re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi; |
||
| 127 | let node = document.createElement('div'); |
||
| 128 | node.classList.add(DIV); |
||
| 129 | const link = document.createElement('a'); |
||
| 130 | const span = document.createElement('span'); |
||
| 131 | const info = document.createElement('i'); |
||
| 132 | const colon = document.createTextNode(': '); |
||
| 133 | const comma = fragment(node, ','); |
||
| 134 | const path = []; |
||
| 135 | const cache = { |
||
| 136 | '{': fragment(node, '{', '}'), |
||
| 137 | '[': fragment(node, '[', ']'), |
||
| 138 | }; |
||
| 139 | |||
| 140 | node.className = `R${ RAND}`; |
||
| 141 | link.classList.add(`L${ RAND}`); |
||
| 142 | if (isEmbed) { |
||
| 143 | |||
| 144 | info.classList.add(`IJSON${ RAND}`); |
||
| 145 | |||
| 146 | } else { |
||
| 147 | |||
| 148 | info.classList.add(`I${ RAND}`); |
||
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | parse(str, re); |
||
| 153 | |||
| 154 | current.innerHTML = node.innerHTML; |
||
| 155 | |||
| 156 | function parse (str, re) { |
||
| 157 | |||
| 158 | str = reconvert(str); |
||
| 159 | let match, val, tmp, i = 0; |
||
| 160 | const len = str.length; |
||
| 161 | try { |
||
| 162 | |||
| 163 | for (; match = re.exec(str);) { |
||
| 164 | |||
| 165 | val = match[0]; |
||
| 166 | if (val == '{' || val == '[') { |
||
| 167 | |||
| 168 | path.push(node); |
||
| 169 | node.appendChild(cache[val].cloneNode(true)); |
||
| 170 | node = node.lastChild.previousSibling; |
||
| 171 | node.len = 1; |
||
| 172 | node.start = re.lastIndex; |
||
| 173 | |||
| 174 | } else if ((val == '}' || val == ']') && node.len) { |
||
| 175 | |||
| 176 | if (node.childNodes.length) { |
||
| 177 | |||
| 178 | tmp = info.cloneNode(); |
||
| 179 | const content = node.len + ( |
||
| 180 | node.len == 1 ? |
||
| 181 | val == ']' ? ' item, ' : ' property, ' : |
||
| 182 | val == ']' ? ' items, ' : ' properties, ' |
||
| 183 | ) + units(re.lastIndex - node.start + 1); |
||
| 184 | |||
| 185 | if (hideDetails) { |
||
| 186 | |||
| 187 | tmp.setAttribute('title', content); |
||
| 188 | |||
| 189 | } else { |
||
| 190 | |||
| 191 | tmp.dataset.content = content; |
||
| 192 | |||
| 193 | } |
||
| 194 | |||
| 195 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 196 | |||
| 197 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, '\\\'')); |
||
| 198 | |||
| 199 | } |
||
| 200 | node.parentNode.insertBefore(tmp, node); |
||
| 201 | |||
| 202 | } else { |
||
| 203 | |||
| 204 | node.parentNode.removeChild(node); |
||
| 205 | |||
| 206 | } |
||
| 207 | node = path.pop(); |
||
| 208 | |||
| 209 | } else if (val == ',') { |
||
| 210 | |||
| 211 | node.len += 1; |
||
| 212 | node.appendChild(comma.cloneNode(true)); |
||
| 213 | |||
| 214 | } else { |
||
| 215 | |||
| 216 | tmp = span.cloneNode(); |
||
| 217 | tmp.textContent = match[1] || val; |
||
| 218 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 219 | node.appendChild(tmp); |
||
| 220 | if (match[3]) { |
||
| 221 | |||
| 222 | node.appendChild(colon.cloneNode()); |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | document.title = ''; |
||
| 230 | JSON.parse(str); |
||
| 231 | |||
| 232 | } catch (e) { |
||
| 233 | |||
| 234 | dlog(e); |
||
| 235 | // TODO: find a better way to report error |
||
| 236 | |||
| 237 | } |
||
| 238 | |||
| 239 | } |
||
| 240 | |||
| 241 | } |
||
| 242 |